local curl = require "lcurl.safe"
local json = require "cjson.safe"
script_info = {
	["title"] = "[伪]度盘搜（Beta2）",
	["description"] = "Beta Version",
	["version"] = "0.0.2",
}

filetype = {
	["Video"] = {"wmv", "rmvb", "mpeg4", "mpeg2", "flv", "avi", "3gp", "mpga", "qt", "rm", "wmz", "wmd", "wvx", "wmx", "wm", "mpg", "mp4", "mkv", "mpeg", "mov", "asf", "m4v", "m3u8", "swf"},
	["Music"] = {"wma", "wav", "mp3", "aac", "ra", "ram", "mp2", "ogg", "aif", "mpega", "amr", "mid", "midi", "m4a", "flac"},
	["Img"] = {"jpg", "jpeg", "gif", "bmp", "png", "jpe", "cur", "svg", "svgz", "ico", "webp", "tif", "tiff"},
	["Rar"] = {"rar","zip","7z","iso"},
	["Exe"] = {"exe"},
	["Apk"] = {"apk"},
	["Txt"] = {"txt","rtf"},
	["Xls"] = {"xls", "xlsx", "xlsm", "xlsb", "csv", "xltx", "xlt", "xltm", "xlam"},
	["Doc"] = {"doc", "docx", "docm", "dotx"},
	["Ppt"] = {"ppt", "pptx", "potx", "pot", "potm", "ppsx", "pps", "ppam", "ppa"},
	["Pdf"] = {"pdf"},
	["Vsd"] = {"vsd"},
	["Torrent"] = {"torrent"},
	["CAD"] = {"dwg","dws","dwt","dxf"},
}

function onSearch(key, page)
	local data = ""
	local c = curl.easy{
		url = "https://admirecn.de/blog/ad/cdn2.php?key=" .. pd.urlEncode(key).. "&page=" .. page,
		followlocation = 1,
		timeout = 15,
		proxy = pd.getProxy(),
		writefunction = function(buffer)
			data = data .. buffer
			return #buffer
		end,
	}
	local _, e = c:perform()
	if e then
        return false
    end
	c:close()
	return parse(data)
end
function onItemClick(item)
	return ACT_SHARELINK, item.url 
end
function getIcon(type)
	for k, v in pairs(filetype) do
		for k2, v2 in pairs(v) do
			if v2 == type then
				return "icon/FileType/Middle/" .. k .. "Type.png"
			end
		end
	end
    return "icon/FileType/Middle/FolderType.png"
end
function getExtension(str)
	_,_,ext =  string.find(str, ".+%.(%w+)$")
    return ext
end

function parse(data)
	local result = {}
	local test = string.sub(data,1,string.len(data)-4)
	local j = json.decode(test)
	if j == nil then
        return false
    end
	for i, w in ipairs(j.data) do
		local tooltip = w.title
        table.insert(result, {["url"] = "https://pan.baidu.com/s/"..w.surl, ["title"] = w.title, ["time"] = time, ["showhtml"] = "true", ["tooltip"] = tooltip, ["check_url"] = "true",["icon_size"] = "28,28",["image"] = getIcon(getExtension(w.title))})
    end
	return result

end